package com.calvin.study.service;

import java.util.concurrent.atomic.LongAdder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class OnlineNumberCounter {

	private LongAdder connections = new LongAdder();

	@Autowired
	private SimpMessagingTemplate template;

	public void increment() {
		connections.increment();
		template.convertAndSend("/topic/getResponse", String.valueOf(connections.sum()));
	}

	public void decrement() {
		connections.decrement();
		template.convertAndSend("/topic/getResponse", String.valueOf(connections.sum()));
	}

	public long onlineUsers() {
		return connections.sum();
	}
}
